package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.CourseDTO;
import com.ktjy.dto.PostDTO;
import com.ktjy.pojo.SysPost;
import com.ktjy.pojo.TbCourse;
import com.ktjy.service.ISysPostService;
import com.ktjy.service.ITbCourseService;
import com.ktjy.utils.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程管理 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/api/tb/course")
public class TbCourseController {
    Logger log= LoggerFactory.getLogger(TbCourseController.class);

    @Autowired
    ITbCourseService courseService;

    @GetMapping("/list")
    public AjaxResult list(CourseDTO courseDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        IPage<TbCourse> page = courseService.findPage(courseDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody TbCourse tbCourse){
        try {
            boolean ok=courseService.saveOrUpdate(tbCourse);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbCourseController中saveOrUpdate方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询详情
        try {
            TbCourse tbCourse=courseService.getById(id);
            if(tbCourse!=null){
                return AjaxResult.success(tbCourse);
            }
        } catch (Exception e) {
            log.error("TbCourseController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= courseService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbCourseController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }
}
